﻿@model BulkEditListModel
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;


    //page title
    ViewBag.Title = T("Admin.Catalog.BulkEdit").Text;
}
@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Administration/Content/images/ico-catalog.png")" alt="" />
            @T("Admin.Catalog.BulkEdit")
        </div>
        <div class="options">
        </div>
    </div>
    
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchProductName):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SearchProductName)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchCategoryId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchCategoryId", Model.AvailableCategories)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchManufacturerId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchManufacturerId", Model.AvailableManufacturers)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchProductTypeId):
            </td>
            <td class="adminData">
                @Html.DropDownList("SearchProductTypeId", Model.AvailableProductTypes)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="search-products" class="k-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                <div id="products-grid"></div>

                <script>
                    $(document).ready(function() {
                        $("#products-grid").kendoGrid({
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: "@Html.Raw(Url.Action("BulkEditSelect", "Product"))",
                                        type: "POST",
                                        dataType: "json",
                                        data: additionalData
                                    },
                                    update: {
                                        url: "@Html.Raw(Url.Action("BulkEditUpdate", "Product"))",
                                        type: "POST",
                                        dataType: "json"
                                    },
                                    destroy: {
                                        url: "@Html.Raw(Url.Action("BulkEditDelete", "Product"))",
                                        type: "POST",
                                        dataType: "json"
                                    },
                                    parameterMap: function(data, operation) {
                                        if (operation != "read") {
                                            //post the entities so the ASP.NET DefaultModelBinder will understand them:
                                            //products[0].Name="name"
                                            //products[0].Id = 1
                                            //products[1].Name="name"
                                            //products[1].Id = 1

                                            var result = {};

                                            for (var i = 0; i < data.models.length; i++) {
                                                var entity = data.models[i];

                                                for (var member in entity) {
                                                    result["products[" + i + "]." + member] = entity[member];
                                                }
                                            }

                                            return result;
                                        } else {
                                            //return JSON.stringify(data);
                                            return data;
                                        }
                                    }
                                },
                                schema: {
                                    data: "Data",
                                    total: "Total",
                                    errors: "Errors",
                                    model: {
                                        id: "Id",
                                        fields: {
                                            Name: { editable: false, type: "string" },
                                            ProductId: { editable: false, type: "number" },
                                            Sku: { editable: true, type: "string" },
                                            Price: { editable: true, type: "number" },
                                            OldPrice: { editable: true, type: "number" },
                                            ManageInventoryMethod: { editable: false, type: "string" },
                                            StockQuantity: { editable: true, type: "number" },
                                            Published: { editable: true, type: "boolean" },
                                            Id: { editable: false, type: "number" }
                                        }
                                    }
                                },
                                requestEnd: function (e) {
                                    if (e.type == "update") {
                                        this.read();
                                    }
                                },
                                error: function(e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                },
                                //bulk editing
                                batch: true,
                                pageSize: @(defaultGridPageSize),
                                serverPaging: true,
                                serverFiltering: true,
                                serverSorting: true
                            },
                            pageable: {
                                refresh: true,
                                pageSizes: [@(gridPageSizes)]
                            },
                            toolbar: ["save", "cancel"],
                            editable: {
                                confirmation: false,
                                //bulk editing
                                mode: "incell"
                            },
                            scrollable: false,
                            columns: [ {
                                    field: "Name",
                                    title: "@T("Admin.Catalog.BulkEdit.Fields.Name")",
                                    width: 200
                                }, {
                                    field: "ProductId",
                                    title: "@T("Admin.Common.View")",
                                    width: 100,
                                    template: '<a href="@Url.Content("~/Admin/Product/Edit/")#=Id#">@T("Admin.Common.View")</a>'
                                },{
                                    field: "Sku",
                                    title: "@T("Admin.Catalog.BulkEdit.Fields.Sku")",
                                    width: 150
                                }, {
                                    field: "Price",
                                    title: "@T("Admin.Catalog.BulkEdit.Fields.Price")",
                                    width: 150,
                                    editor: function (container, options) {
                                        $('<input name="' + options.field + '"/>')
                                            .appendTo(container)
                                            .kendoNumericTextBox({
                                                format: "{0:n4}",
                                                decimals: 4
                                            });
                                    }
                                }, {
                                    field: "OldPrice",
                                    title: "@T("Admin.Catalog.BulkEdit.Fields.OldPrice")",
                                    width: 150,
                                    editor: function (container, options) {
                                        $('<input name="' + options.field + '"/>')
                                            .appendTo(container)
                                            .kendoNumericTextBox({
                                                format: "{0:n4}",
                                                decimals: 4
                                            });
                                    }
                                }, {
                                    field: "ManageInventoryMethod",
                                    title: "@T("Admin.Catalog.BulkEdit.Fields.ManageInventoryMethod")",
                                    width: 150
                                }, {
                                    field: "StockQuantity",
                                    title: "@T("Admin.Catalog.BulkEdit.Fields.StockQuantity")",
                                    //integer format
                                    format: "{0:0}",
                                    width: 150
                                },{
                                    field: "Published",
                                    title: "@T("Admin.Catalog.BulkEdit.Fields.Published")",
                                    width: 200
                                }, {
                                    command: { name: "destroy", text: "@T("Admin.Common.Delete")" }, 
                                    title: "@T("Admin.Common.Delete")",
                                    width: 100
                                }
                            ]
                        } );
                    });
                </script>

                <script type="text/javascript">

                    $(document).ready(function () {
                        $('#search-products').click(function () {
                            var grid = $('#products-grid').data('kendoGrid');
                            grid.dataSource.page(1); //new search. Set page size to 1
                            grid.dataSource.read();
                            return false;
                        });
        
                        $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
                            if (event.keyCode == 13) {
                                $("#search-products").click();
                                return false;
                            }
                        });
                    });

                    function additionalData() {
                        return {
                            SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
                            SearchCategoryId: $('#SearchCategoryId').val(),
                            SearchManufacturerId: $('#SearchManufacturerId').val(),
                            SearchProductTypeId: $('#SearchProductTypeId').val()
                        };
                    }
                </script>
            </td>
        </tr>
    </table>
}